package com.hardy.string;

public class StringArrMaxMinData{
	//�ж��ַ����Ƿ��ǺϷ�������
    public static boolean isValid(char[] chas){
    	//����ַ���������-��ͷ������0-9�е����ֿ�ͷ�����Ϸ�������false
    	if(chas[0]!='-'&&(chas[0]<'0')||chas[0]>'9'){
    		return false;
    	}
    	//�ַ���-0����-���Ϸ�
    	if(chas[0]=='-'&&(chas.length==1||chas[1]=='0')){
    		return false;
    	}
    	//�ַ�����0��ͷ���Ϸ�
    	if(chas[0]=='0'&&chas.length>1){
    		return false;
    	}
    	for(int i=1;i<chas.length;i++){
    		if(chas[i]<'0'||chas[i]>'9'){
    			return false;
    		}
    	}
    	return true;
    }
    public static Double[] randomCommon(Double min, Double max, int n){  
        if (n > (max - min + 1) || max < min) {  
               return null;  
        }  
        Double[] result = new Double[n];  
        int count = 0;  
        while(count < n) {  
        	Double num = (Double) (Math.random() * (max - min)) + min;  
            boolean flag = true;  
            for (int j = 0; j < n; j++) {  
                if(num == result[j]){  
                    flag = false;  
                    break;  
                }  
            }  
            if(flag){  
                result[count] = num;  
                count++;  
            }  
        }  
        return result;  
    }  
    public static Double findMaxMethod(Double[] pArr,int pStart,int pEnd){
        Double max;
    	if(pEnd-pStart<=1){
    		if(pArr[pStart]>pArr[pEnd]){
    			max=pArr[pStart];
    		}else{
    			max=pArr[pEnd];
    		}
    		return max;
    	}
    	Double leftMax=findMaxMethod(pArr, pStart, pStart+(pEnd-pStart)/2);
    	Double rightMax=findMaxMethod(pArr, pStart+(pEnd-pStart)/2+1, pEnd);
    	max=leftMax>rightMax?leftMax:rightMax;
    	return max;
    }
    public static Double findMaxMethod1(Double[] pArr){
    	Double max=pArr[0];
    	Double min=pArr[0];
    	for(int i=0;i<pArr.length;i++){
    		if(pArr[i]>max){
    			max=pArr[i];
    		}
    	}
    	return max;
    }
    public static Double findMax(Double[] arr,int l,int r){
		Double max = 0.0;
		if (l == r) {
			max = arr[l];
		} else if (l + 1 == r) {
			if (arr[l] < arr[r]) {
				max = arr[r];
			} else {
				max = arr[l];
			}
		} else {
			int mid = (l + r) / 2;
			Double preHalf = findMax(arr, l, mid);
			Double postHalf = findMax(arr, mid + 1, r);
			max = preHalf > postHalf ? preHalf : postHalf;
		}
		return max;

    }
    public static void main(String[] args) {
    	int n=1000000;
    	Double[] pArr=new Double[n];
    	for(int i=0;i<pArr.length;i++){
    		pArr[i]=(double) i;
    	}
		//Double[] dArr=randomCommon(0d,500000d,50000);
		long startTime=System.currentTimeMillis();
		System.out.println("startTime===="+startTime);
		System.out.println("���ֵΪ"+findMax(pArr, 0, pArr.length-1));
		long endTime=System.currentTimeMillis();
		System.out.println("endTime====="+endTime);
		System.out.println(endTime-startTime);
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		long start=System.currentTimeMillis();
		System.out.println("-----start"+start);
		System.out.println("���ֵΪ"+findMaxMethod1(pArr));
		long end=System.currentTimeMillis();
		System.out.println("------end"+end);
		System.out.println(end-start);
	}
    
}
